package org.itboys.mobile.controller.mobile.promotion;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.promotion.SundayCouponDto;
import org.itboys.mobile.service.mysql.mobile.promotion.SundayMobilePromotionService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 作者：zq
 * 时间：2017年5月17日
 * 描述：圣代_移动端_促销(暂时只有优惠券)_controller
 */
@Controller
@RequestMapping("/sunday/mobile/promotion")
public class SundayMobilePromotionCouponController extends BaseController{
    @Autowired
    private SundayMobilePromotionService promotionService;
   /**
     * 分页获取用户的优惠券
     * @param request
     * @param response
     * @param pageSize
     * @param pageNumber
     * @param sort
     * @param order
     */
    @RequestMapping(value="/coupon/getMemberCoupon",method= RequestMethod.POST)
    @ApiOperation(value="获取用户优惠券。分页")
    public void getMemberCoupon(HttpServletRequest request,
                       HttpServletResponse response,
                        @ApiParam(required=true, name="status", value="优惠券状态。1未使用，2已使用，3已过期，0全部")@RequestParam(value = "status", required = false)Integer status,
                       @ApiParam(required=false, name="amount", value="金额。结算时需要付款的实际金额。系统会根据此金额过滤掉不符合条件的优惠券")@RequestParam(value = "amount", required = false)BigDecimal amount,
                       @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
                       @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = false) String pageNumber,
                       @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
                       @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
                        @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                        @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign

                       ){
                    try{
                        Long memberId = AccountIdHoldUtils.getMemberId();
                        List<SundayCouponDto> result=promotionService.getMemberCoupon(request,memberId,status,amount);
                   AjaxUtils.renderJsonporJson(response, null,
                                ResultPageDOHelper.getMsgResultDO(result, CommonConstants.SUCCESS));
                    }catch (Exception e){
                        String msg ="";
                        if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                            msg=e.getMessage();
                        }else{
                            msg= CommonConstants.ERROMSG;
                            logger.info("/sunday/mobile/promotion/coupon/getMemberCoupon--------"+e);
                        }
                        e.printStackTrace();
                        AjaxUtils.renderJsonporJson(response, null,
                                ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
                    }
    }
}
